﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace Continental.Admin
{
    public partial class ManagerUser : System.Web.UI.Page
    {
        public Int32 amountOfUser = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlRole.DataSource = Roles.GetAllRoles();
                ddlRole.DataBind();
            }

            this.lvUser.DataSource = GetUserByRole(ddlRole.SelectedValue);
            this.lvUser.DataBind();
            amountOfUser = lvUser.Items.Count;
        }

        private MembershipUserCollection GetUserByRole(String roleName)
        {
            MembershipUserCollection userList = new MembershipUserCollection();
            MembershipUserCollection allUser = Membership.GetAllUsers();
            if (roleName != "all")
            {
                foreach (MembershipUser theUser in allUser)
                {
                    if (Roles.IsUserInRole(theUser.UserName, roleName))
                    {
                        userList.Add(theUser);
                    }
                }
                return userList;
            }
            return allUser;
        }

        protected void dpUser_PreRender(object sender, EventArgs e)
        {
            lvUser.DataSource = GetUserByRole(ddlRole.SelectedValue);
            lvUser.DataBind();
        }

        protected void ddlRole_DataBound(object sender, EventArgs e)
        {
            ListItem theItem = new ListItem("Tất cả", "all");
            ddlRole.Items.Insert(0, theItem);
        }


    }
}
